package cn.school.workflow.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.school.common.utils.SysUserUtils;
import cn.school.workflow.dao.RepairApplyDao;
import cn.school.workflow.service.RepairApplyService;
import cn.school.workflow.service.WorkFlowCommonService;
import cn.school.workflow.vo.RepairApplyVO;
import cn.school.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class RepairApplyServiceImpl extends ServiceImpl<RepairApplyDao, RepairApplyVO> implements RepairApplyService {

    @Autowired
    private WorkFlowCommonService workFlowCommonService;

    @Override
    public IPage<RepairApplyVO> findRepairApplyPageList(RepairApplyVO vo) {
        LambdaQueryWrapper<RepairApplyVO> wrapper = new LambdaQueryWrapper<>();
        //默认只能查看到自己提交的申请单
        wrapper.eq(RepairApplyVO::getCreateBy,SysUserUtils.getSysUser().getUsername());
        if(!StringUtils.isEmpty(vo.getApplyNo())){
            wrapper.eq(RepairApplyVO::getApplyNo,vo.getApplyNo());
        }
        if(!StringUtils.isEmpty(vo.getArticleName())){
            wrapper.like(RepairApplyVO::getArticleName,vo.getArticleName());
        }
        if(!StringUtils.isEmpty(vo.getDamageTime())){
            wrapper.like(RepairApplyVO::getDamageTime,vo.getDamageTime());
        }
        if(!StringUtils.isEmpty(vo.getApplyStatus())){
            wrapper.like(RepairApplyVO::getApplyStatus,vo.getApplyStatus());
        }
        wrapper.eq(RepairApplyVO::getTenantId, SysUserUtils.getSysUser().getTenantId());
        wrapper.orderByDesc(RepairApplyVO::getCreateTime);
        return page(new Page<>(vo.getPageNum(), vo.getPageSize()), wrapper);
    }

    @Override
    public boolean cancelRepairApply(WorkFlowVO vo) {
        LambdaQueryWrapper<RepairApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RepairApplyVO::getProcessId,vo.getProcessInstanceId());
        RepairApplyVO applyVO = new RepairApplyVO();
        applyVO.setApplyStatus("4");//撤销流程
        this.baseMapper.update(applyVO,wrapper);
        return workFlowCommonService.cancelApply(vo.getProcessInstanceId(),vo.getApplyDesc());
    }

    @Override
    public int createRepairApply(RepairApplyVO vo) {
        String username = SysUserUtils.getSysUser().getUsername();

        String applyNo = "RA_"+ DateUtil.format(new Date(),"yyyyMMddHHmmssSSS");
        vo.setApplyNo(applyNo);
        vo.setCreateBy(username);
        vo.setCreateTime(new Date());
        //创建申请单
        this.baseMapper.insert(vo);

        WorkFlowVO flowVO = new WorkFlowVO();
        flowVO.setApplyTitle("维修处理流程");
        flowVO.setApplyID("repairApply");
        flowVO.setBusinessKey(applyNo);
        Map<String,Object> variablesMap = new HashMap<>();
        variablesMap.put("assignee",vo.getCurrApprovedBy());
        flowVO.setVariables(variablesMap);
        //创建请假工作流
        String processInstanceId = workFlowCommonService.createProcessInstance(flowVO);
        vo.setProcessId(processInstanceId);
        //更新流程实例ID
        return this.baseMapper.updateById(vo);
    }

    @Override
    public RepairApplyVO findRepairApplyDetail(Long processInstanceId) {
        LambdaQueryWrapper<RepairApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RepairApplyVO::getProcessId,processInstanceId);
        return this.baseMapper.selectOne(wrapper);
    }
}
